const { arcgisToGeoJSON } = require("@esri/arcgis-to-geojson-utils");

function polylineToGeoJson(polyline) {
  const coordinates = [];
  for (let item1 of polyline.split("|")) {
    const points = [];
    for (let item2 of item1.split(";")) {
      const point = [];
      for (let item3 of item2.split(",")) {
        point.push(+item3);
      }
      points.push(point);
    }
    coordinates.push(points);
  }
  const json = { rings: coordinates, spatialReference: { wkid: 4326 } };

  const geojson = arcgisToGeoJSON(json);
  return geojson;
}

module.exports = { polylineToGeoJson };
